package com.zjy.controller;

import com.zjy.pojo.dto.UserDto;
import com.zjy.service.UserService;
import com.zjy.pojo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public ResponseResult<UserDto> login(@RequestBody UserDto user) {
        return ResponseResult.success(
                UserDto.convertBy(userService.login(user.getUsername(), user.getPassword())));
    }

    @PostMapping("/register")
    public ResponseResult<UserDto> register(@RequestBody UserDto user) {
        return ResponseResult.success(UserDto.convertBy(userService.register(user)));
    }

    @GetMapping("/profile/{userId}")
    public ResponseResult<UserDto> getProfile(@PathVariable Long userId) {
        return ResponseResult.success(UserDto.convertBy(userService.getProfile(userId)));
    }

    @PutMapping("/profile/{userId}")
    public ResponseResult<UserDto> updateProfile(@PathVariable Long userId,
                                              @RequestBody UserDto user) {
        return ResponseResult.success(
                UserDto.convertBy(userService.updateProfile(userId, user)));
    }
}